package com.x.erp.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.x.erp.model.District;
import com.x.framework.dao.BaseDao;

@Repository
public class DistrictDao extends BaseDao {
    private final static String selectDistrictSql = "SELECT D.* FROM BASE_DISTRICT D, BASE_CITY C, BASE_PROVINCE P " +
            "WHERE D.CITY_ID = C.CITY_ID AND C.PROVINCE_ID = P.PROVINCE_ID ";

    public List<District> selectDistrictList(District district) throws Exception {
        StringBuilder sb = new StringBuilder(selectDistrictSql);
        List<Object> params = new ArrayList<Object>();
        List<District> result = new ArrayList<District>();
        if (this.isString(district.getDistrictId())) {
            sb.append("AND D.DISTRICT_ID = ? ");
            params.add(district.getDistrictId());
        }
        if (this.isString(district.getDistrictCode())) {
            sb.append("AND D.DISTRICT_CODE = ? ");
            params.add(district.getDistrictCode());
        }
        if (this.isString(district.getDistrictName())) {
            sb.append("AND D.DISTRICT_NAME = ? ");
            params.add(district.getDistrictName());
        }
        if (district.getDistrictSeq() != null) {
            sb.append("AND D.DISTRICT_SEQ = ? ");
            params.add(district.getDistrictSeq());
        }
        if (district.getDistrictState() != null) {
            sb.append("AND D.DISTRICT_STATE = ? ");
            params.add(district.getDistrictState());
        }
        if (this.isString(district.getDistrictPinyin())) {
            sb.append("AND D.DISTRICT_PINYIN LIKE ? ");
            params.add(district.getDistrictPinyin() + "%");
        }
        if (this.isString(district.getDistrictInitial())) {
            sb.append("AND D.DISTRICT_INITIAL LIKE ? ");
            params.add(district.getDistrictInitial() + "%");
        }
        if (this.isString(district.getCityId())) {
            sb.append("AND D.CITY_ID = ? ");
            params.add(district.getCityId());
        }
        if (this.isString(district.getProvinceName())) {
            sb.append("AND P.PROVINCE_NAME = ? ");
            params.add(district.getProvinceName());
        }
        if (this.isString(district.getCityName())) {
            sb.append("AND C.CITY_NAME = ? ");
            params.add(district.getCityName());
        }
        result = this.queryForList(sb.toString(), District.class, params);
        return result;
    }

}
